/*
 * Filename: g:\espidf_workspace\idf_S3_T-Watch\main\sources\rtctime.c
 * Path: g:\espidf_workspace\idf_S3_T-Watch\main\sources
 * Created Date: Thursday, June 19th 2025, 4:33:27 pm
 * Author: sss
 *
 * Copyright (c) 2025 Your Company
 */
#include "time.h"
#include "rtctime.h"
#include "SensorPCF8563.hpp"
#include "esp_log.h"
#include "esp_err.h"

static const char *TAG = "RTC_TIME";
static time_t current_time;
static struct tm timeinfo_storage;
static struct tm* timeinfo = &timeinfo_storage;

static SensorPCF8563 rtc;

void rtc_init(i2c_master_bus_handle_t bus)
{

    if (rtc.begin(i2c_bus_handle, PCF8563_SLAVE_ADDRESS))
    {
        ESP_LOGI(TAG, "rtc begin success");
        // rtc_inited = true;
        rtc.getDateTime(timeinfo);

        ESP_LOGI(TAG, "rtc current time %d-%d-%d %d:%d:%d", timeinfo->tm_year + 1900, timeinfo->tm_mon + 1, timeinfo->tm_mday, timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec);

        set_sys_time(timeinfo);
    }
}

void rtc_set_time(time_t t)
{
    // TODO: Implement this function
}

time_t rtc_get_time(void)
{
    // TODO: Implement this function
    return 0;
}

void set_sys_time(tm* timeinfo)
{
    // TODO: Implement this function
    time_t t = mktime(timeinfo);
    struct timeval now = {.tv_sec = t};
    settimeofday(&now, NULL);

    ESP_LOGI("RTC", "Internal RTC set to: %s", asctime(timeinfo));
}